package com.zmn.plat.services.impl.base.fault;

import com.zmn.common.utils.date.DateUtil;


import com.zmn.plat.model.entity.base.fault.Fault;
import com.zmn.plat.model.entity.base.fault.FaultQuery;
import com.zmn.plat.persistence.interfaces.base.fault.FaultDao;
import com.zmn.plat.services.interfaces.base.fault.FaultService;
import org.springframework.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import com.zmn.common.constant.GlobalConsts;

/**
 * 描述: 故障管理接口实现类
 *
 * @author chengguang
 * @since  2021/11/17 15:31
 */
@Slf4j
@Service
public class FaultServiceImpl implements FaultService {

    private static final String TAG = "故障管理基础服务接口";

    @Resource
    private FaultDao faultDao;

    @Override
    public Fault findById(Integer faultId) {
        if (Objects.isNull(faultId)) {
            log.error("[{}],根据id查询数据faultId为null", TAG);
            return null;
        }
        return faultDao.findById(faultId);
    }

    @Override
    public List<Fault> listByIds(List<Integer> faultIds) {
        if (CollectionUtils.isEmpty(faultIds)) {
            log.error("[{}],按条件查询数据,faultIds为null", TAG);
            return null;
        }
        return faultDao.listByIds(faultIds);
    }

    @Override
    public List<Fault> listByQuery(FaultQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return faultDao.listByQuery(query);
    }

    @Override
    public List<Fault> listPageByQuery(FaultQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(faultDao.countByQuery(query));
        return faultDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(FaultQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return faultDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(Fault fault) {
        if (Objects.isNull(fault.getFaultId())){
            log.error("[{}],根据id修改数据,Fault为null", TAG);
            return null;
        }
        return faultDao.updateByKey(fault);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {
        if (!Objects.equals(status, GlobalConsts.NO) && !Objects.equals(status, GlobalConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        faultDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(Fault fault){
        if (Objects.isNull(fault)){
            log.error("[{}],新增数据,Fault为null", TAG);
            return null;
        }
        return faultDao.insert(fault);
    }

    @Override
    public void insertBatch(List<Fault> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        faultDao.insertBatch(list);
    }

    @Override
    public void insertBatchExport(List<Fault> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        faultDao.insertBatchExport(list);
    }

    @Override
    public Integer deleteByKey(Integer faultId) {
        if (Objects.isNull(faultId)){
            log.error("[{}],根据id删除数据,faultId为null", TAG);
            return null;
        }
        return faultDao.deleteByKey(faultId);
    }

    @Override
    public Integer getMaxSort() {
        return faultDao.getMaxSort();
    }

}